package GUILayer;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 

public class JSearchTextField extends JIconTextField implements FocusListener, MouseListener, MouseMotionListener, ActionListener  {
	private static final long serialVersionUID = 1L;
	
	private String textWhenNotFocused;
	private IFSearch submitM;
	
    public JSearchTextField(IFSearch submitM) {
        super();
        
        this.submitM = submitM;
        this.setIcon(new ImageIcon(getClass().getResource("/icons/find-16x16.png")));
        this.textWhenNotFocused = "Search...";
        this.addFocusListener(this);
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
        this.addActionListener(this);
    }
 
    public String getTextWhenNotFocused() {
        return this.textWhenNotFocused;
    }
 
    public void setTextWhenNotFocused(String newText) {
        this.textWhenNotFocused = newText;
    }
 
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
 
        if (!this.hasFocus() && this.getText().equals("")) {
            int height = this.getHeight();
            Font prev = g.getFont();
            Font italic = prev.deriveFont(Font.ITALIC);
            Color prevColor = g.getColor();
            g.setFont(italic);
            g.setColor(UIManager.getColor("textInactiveText"));
            int h = g.getFontMetrics().getHeight();
            int textBottom = (height - h) / 2 + h - 4;
            int x = this.getInsets().left;
            Graphics2D g2d = (Graphics2D) g;
            RenderingHints hints = g2d.getRenderingHints();
            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            g2d.drawString(textWhenNotFocused, x, textBottom);
            g2d.setRenderingHints(hints);
            g.setFont(prev);
            g.setColor(prevColor);
        }
 
    }
    
    
    private void submit(){
    	submitM.submit();
    }
 
	//FocusListener implementation:
    public void focusGained(FocusEvent e) {
        this.repaint();
    }
 
    public void focusLost(FocusEvent e) {
        this.repaint();
    }
    
	//MouseListener implementation:
	public void mousePressed(MouseEvent e) {
		if(e.getPoint().x < getIcon().getIconWidth()) 
			submit();
	}
	
	//ActionListener implementation:
	public void actionPerformed(ActionEvent e) {
		submit();
	}
	
	//MouseMotionListener implementation:
	public void mouseMoved(MouseEvent e) {
		if(e.getPoint().x < getIcon().getIconWidth()) 
			this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		else
			this.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
	}
	
	//Other interface methods implementation:
	public void mouseEntered(MouseEvent e) {}
	public void mouseClicked(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mouseDragged(MouseEvent e) {}
}